package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_member_point;

import cn.com.api.service.ApiT_member_pointService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "t_member_point/")
public class ApiT_member_pointController {
    @Autowired
    private ApiT_member_pointService apiT_member_pointService;
	
	private static Logger logger = Logger.getLogger(ApiT_member_pointController.class);
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertT_member_point")
	public void insertT_member_point(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String member_id = request.getParameter("member_id");
		String point = request.getParameter("point");
		String change_point = request.getParameter("change_point");
		String consume_point = request.getParameter("consume_point");
		String addtime = request.getParameter("addtime");
		String expiretime = request.getParameter("expiretime");
		String reason = request.getParameter("reason");
		String remark = request.getParameter("remark");
		String related_id = request.getParameter("related_id");
		String type = request.getParameter("type");
		String operator = request.getParameter("operator");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiT_member_point t_member_point = new ApiT_member_point();
				
				/*
    		    t_member_point.setId(id);
    		    t_member_point.setMember_id(member_id);
    		    t_member_point.setPoint(point);
    		    t_member_point.setChange_point(change_point);
    		    t_member_point.setConsume_point(consume_point);
    		    t_member_point.setAddtime(addtime);
    		    t_member_point.setExpiretime(expiretime);
    		    t_member_point.setReason(reason);
    		    t_member_point.setRemark(remark);
    		    t_member_point.setRelated_id(related_id);
    		    t_member_point.setType(type);
    		    t_member_point.setOperator(operator);
				*/
				apiT_member_pointService.insertT_member_point(t_member_point);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_member_pointById")
	public void findT_member_pointById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiT_member_point t_member_point = apiT_member_pointService.findT_member_pointById(Integer.valueOf(id));
		
			// return
            map.put("datas", t_member_point);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_member_points")
	public void findT_member_points(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiT_member_point> t_member_points = apiT_member_pointService.findT_member_points();
		
			// return
            map.put("datas", t_member_points);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateT_member_point")
	public void updateT_member_point(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String member_id = request.getParameter("member_id");
		String point = request.getParameter("point");
		String change_point = request.getParameter("change_point");
		String consume_point = request.getParameter("consume_point");
		String addtime = request.getParameter("addtime");
		String expiretime = request.getParameter("expiretime");
		String reason = request.getParameter("reason");
		String remark = request.getParameter("remark");
		String related_id = request.getParameter("related_id");
		String type = request.getParameter("type");
		String operator = request.getParameter("operator");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiT_member_point t_member_point = new ApiT_member_point();
				//获取原数据
				t_member_point  = apiT_member_pointService.findT_member_pointById(Integer.valueOf(id));
				//修改
				//t_member_point.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiT_member_pointService.updateT_member_point(t_member_point);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteT_member_point")
	public void deleteT_member_point(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiT_member_pointService.deleteT_member_point(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "删除成功！");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "检查失败！");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
 
}


